package com.danawa.watcher.front;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.danawa.watcher.front.serviceStrategy.WatcherServiceStrategy;
import com.danawa.watcher.util.StringUtil;
import com.danawa.watcherApi.database.command.common.Operator;
import com.danawa.watcherApi.database.domain.BaseProduct;
import com.danawa.watcherApi.database.domain.InflowProduct;
import com.danawa.watcherApi.database.domain.MemberTracking;
import com.danawa.watcherApi.database.domain.PriceHistory;
import com.danawa.watcherApi.database.service.WatcherService;
import com.danawa.watcherApi.parser.ShopParser;

@Controller 	
@RequestMapping("prod")
public class ProdController {
	
	@Autowired 
	WatcherService watcherService;
	
	@Autowired 
	WatcherServiceStrategy watcherServiceStrategy;
	
	@RequestMapping(value ="/addSiteUrl" , method = RequestMethod.GET)
	public void addSiteUrl(@RequestParam(value = "siteUrl", required = true ) String siteUrl,
			ModelMap modelMap, HttpServletRequest request) throws Throwable{	

		
		Map<String, String> parserData = watcherServiceStrategy.dataParserMap(siteUrl);
		
		int baseProductSeq = 0;
		if(parserData != null){
			String key = parserData.get(ShopParser.PROD_CODE);
			BaseProduct baseProduct = new BaseProduct();
			baseProduct.setOperator(Operator.EQUAL, "key", key);
			List<BaseProduct> baseProducts = watcherService.getBaseProducts(baseProduct);
			
			//이미 상품 등록되어있는지 체크
			if(baseProducts.size() > 0){
				baseProductSeq = baseProducts.get(0).getBaseProductSeq();
			}else{
				//상품등록이 안되어있다면 상품 등록
				baseProductSeq = watcherServiceStrategy.addProductProcess(parserData);
			}
		}
		modelMap.addAttribute("result", baseProductSeq);
	}
	
	@RequestMapping(value ="/prodView" , method = RequestMethod.GET)
	public String prodView(@RequestParam(value = "baseProductSeq", required = true ) int baseProductSeq,
			@RequestParam(value = "month", required = false, defaultValue = "12" ) int month,
			@RequestParam(value = "siteTypeIndex", required = false, defaultValue = "0" ) int siteTypeIndex,
			ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) throws Throwable{	
		
		BaseProduct baseProduct = watcherServiceStrategy.selectProductProcess(baseProductSeq);
	
		/**
		 * 상품 가격 업데이트
		 */
		if(baseProduct != null){
			watcherServiceStrategy.updateProductProcess(baseProduct);
		}
		
		/**
		 * 회원 트래킹 정보
		 */
		int memberSeq = 1; //회원번호
		List<MemberTracking> memberTrackings = watcherServiceStrategy.memberTracking(memberSeq, baseProduct);
		
		
		//modelMap.addAttribute("inflowProduct", baseProduct.getInflowProducts().get(siteTypeIndex));
		modelMap.addAttribute("baseProduct", baseProduct);
		modelMap.addAttribute("memberTrackings", memberTrackings);
		return "prod/prodView";
		
	}
	
}
